﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GlassStore.Models;
using GlassStore.ViewModels;
using GlassStore.Helpers;

namespace GlassStore.Controllers
{
    [Authorize(Roles = "User")]
    public class GlassController : Controller
    {
        GlassStoreEntities storeDB = new GlassStoreEntities();
        GlassHelper glassHelper = new GlassHelper();
        private int pagesize = 16;

        //
        // GET: /Glass/

        public ActionResult Index()
        {
            var glassViewModel = new GlassIndexViewModel
                {
                    TopSaleGlasses = storeDB.Glasses.Where(g => g.TopSale > 0).OrderBy(g => g.TopSale).ToList(),
                    TopNewGlasses = storeDB.Glasses.Where(g => g.TopNew > 0).OrderBy(g => g.TopNew).ToList(),
                    TopBargainGlasses = storeDB.Glasses.Where(g => g.TopBargain > 0).OrderBy(g => g.TopBargain).ToList(),
                };

            return View(glassViewModel);
        }

        //
        // GET: /Glass/Browse?cId=5&id=1
        public ActionResult Browse(int cId, int?id)
        {
            int index = id.HasValue ? id.Value : 1;
            var glasses = storeDB.GlassCatalogues.Include("Glass").Where(gc => gc.SubCatalogueId == cId).Select(gc => gc.Glass);
            var viewModel = new GlassBrowseViewModel
            {
                Glasses = glasses.OrderBy(g => g.GlassId).Skip((index - 1) * pagesize).Take(pagesize).ToList(),
                SubCatalogue = storeDB.SubCatalogues.Single(sub => sub.SubCatalogueId == cId),
                Index = index,
                Total = (glasses.Count() - 1) / pagesize + 1,
            };
            return View(viewModel);
        }

        //
        // GET: /Glass/BrowseProperty?p=Material&v=钛架&id=1
        public ActionResult BrowseProperty(string p, string v, int?id)
        {
            int index = id.HasValue ? id.Value : 1;
            IQueryable<Glass> glasses = null;
            if (p.ToLower().Equals("material"))
            {
                glasses = storeDB.Glasses.Where(g => g.Material == v);
            }
            else if (p.ToLower().Equals("styledetail"))
            {
                glasses = storeDB.Glasses.Where(g => g.StyleDetail == v);
            }
            var viewModel = new GlassBrowsePropertyViewModel
            {
                Glasses = glasses.OrderBy(g => g.GlassId).Skip((index - 1) * pagesize).Take(pagesize).ToList(),
                Index = index,
                Total = (glasses.Count() - 1) / pagesize + 1,
                P = p,
                V = v,
            };
            return View(viewModel);
        }

        //
        // GET: /Glass/Search?keyword=1&price1=0&price2=1000&id=1
        public ActionResult Search(string keyword, string price1, string price2, int?id)
        {
            if (string.IsNullOrEmpty(keyword))
                keyword = string.Empty;
            keyword = keyword.Trim().ToLower();
            double p1 = 0, p2 = double.MaxValue;
            double.TryParse(price1, out p1);
            double.TryParse(price2, out p2);

            int index = id.HasValue ? id.Value : 1;
            var glasses = storeDB.Glasses.Where(g => g.GlassName.ToLower().Contains(keyword) && g.Price >= p1 && g.Price <= p2);
            var viewModel = new GlassSearchViewModel
            {
                Glasses = glasses.OrderBy(g => g.GlassId).Skip((index - 1) * pagesize).Take(pagesize).ToList(),
                Index = index,
                Total = (glasses.Count() - 1) / pagesize + 1,
                Price1 = price1,
                Price2 = price2,
            };
            return View(viewModel);
        }

        //
        // GET: /Glass/Details/5
        public ActionResult Details(int id)
        {
          var glass = storeDB.Glasses.Include("GlassPictures").Single(g => g.GlassId == id);
          return View(glass);
        }

        [ChildActionOnly]
        public ActionResult CatalogueMenu()
        {
          var catalogues = storeDB.TopCatalogues.Include("SubCatalogues").ToList();
          return View(catalogues);
        }

        [ChildActionOnly]
        public ActionResult PropertyMenu()
        {
            var propertyViewModel = new GlassPropertyMenuViewModel
            {
                MaterialList = glassHelper.GetMaterialList(),
                OpticsGlasses = glassHelper.GetOpticsGlasses(),
                SunGlasses = glassHelper.GetSunGlasses(),
            };
            return View(propertyViewModel);
        }
    }
}
